namespace SeataNet.Core.Attributes
{
    /// <summary>
    /// 全局事务特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class GlobalTransactionalAttribute : Attribute
    {
        /// <summary>
        /// 事务超时时间（毫秒）
        /// </summary>
        public int Timeout { get; set; } = 60000;

        /// <summary>
        /// 事务超时时间（毫秒）- 别名
        /// </summary>
        public int TimeoutMs
        {
            get => Timeout;
            set => Timeout = value;
        }

        /// <summary>
        /// 事务超时时间（毫秒）- 别名
        /// </summary>
        public int TimeoutMilliseconds
        {
            get => Timeout;
            set => Timeout = value;
        }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int Retry { get; set; } = 0;

        /// <summary>
        /// 事务名称
        /// </summary>
        public string? Name { get; set; }

        /// <summary>
        /// 回滚的异常类型
        /// </summary>
        public Type[]? RollbackFor { get; set; }

        /// <summary>
        /// 不回滚的异常类型
        /// </summary>
        public Type[]? NoRollbackFor { get; set; }

        /// <summary>
        /// 事务类型
        /// </summary>
        public string TransactionType { get; set; } = "AT";

        /// <summary>
        /// 事务传播行为
        /// </summary>
        public TransactionPropagationBehavior Propagation { get; set; } = TransactionPropagationBehavior.Required;
    }
    
    /// <summary>
    /// 事务传播行为枚举
    /// </summary>
    public enum TransactionPropagationBehavior
    {
        /// <summary>
        /// 支持当前事务，如果不存在则创建一个新事务
        /// </summary>
        Required = 0,
        
        /// <summary>
        /// 支持当前事务，如果不存在则以非事务方式执行
        /// </summary>
        Supports = 1,
        
        /// <summary>
        /// 支持当前事务，如果不存在则抛出异常
        /// </summary>
        Mandatory = 2,
        
        /// <summary>
        /// 以非事务方式执行，如果存在当前事务则挂起
        /// </summary>
        RequiresNew = 3,
        
        /// <summary>
        /// 以非事务方式执行，如果存在当前事务则抛出异常
        /// </summary>
        NotSupported = 4,
        
        /// <summary>
        /// 以非事务方式执行，忽略当前事务
        /// </summary>
        Never = 5,
        
        /// <summary>
        /// 嵌套事务
        /// </summary>
        Nested = 6
    }
}